package com.mt.mtyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.acl.service.RoleService;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.entity.acl.Role;
import com.mt.mtyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author WMT
 * @Date 2023/6/14 20:13
 */
@Api(tags = "角色模块")
@RestController
@RequestMapping("/admin/acl/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("分页条件查询角色列表")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<Role>> roleList(
            @PathVariable Long page,
            @PathVariable Long limit,
            RoleQueryVo roleQueryVo
    ) throws Exception {
        // 1.封装分页条件
        Page<Role> rolePage = new Page<>(page, limit);
        // 2.调用分页查询方法
        return Result.success(roleService.getRoleList(rolePage, roleQueryVo));
    }

    @ApiOperation("根据id查询角色详情")
    @GetMapping("/get/{id}")
    public Result<Role> getRoleById(@PathVariable Long id) throws Exception {
        return Result.success(roleService.getById(id));
    }


    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Role role) throws Exception {
        if (StringUtils.isEmpty(role.getRoleName())) {
            throw new BusinessException("角色名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (roleService.selectCountByName(role.getRoleName()) > 0) {
            throw new BusinessException("角色名称已存在！", HttpStatus.BAD_REQUEST.value());
        }
        LocalDateTime now = LocalDateTime.now();
        role.setCreateTime(now);
        role.setUpdateTime(now);
        return Result.success(roleService.insert(role));
    }

    @ApiOperation("根据id删除角色")
    @PostMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(roleService.delete(id));
    }

    @ApiOperation("修改角色")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Role role) throws Exception {
        if (StringUtils.isEmpty(role.getId())) {
            if (StringUtils.isEmpty(role.getId())) {
                throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
            }
        }
        if (StringUtils.isEmpty(role.getRoleName())) {
            throw new BusinessException("角色名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        Role roleDb = roleService.getById(role.getId());
        if (roleDb != null) {
            if (!roleDb.getRoleName().equals(role.getRoleName())) {
                if (roleService.selectCountByName(role.getRoleName()) > 0) {
                    throw new BusinessException("角色名称已存在！", HttpStatus.BAD_REQUEST.value());
                }
            }
        }
        LocalDateTime now = LocalDateTime.now();
        role.setUpdateTime(now);
        return Result.success(roleService.update(role));
    }

    @ApiOperation("批量删除角色")
    @PostMapping("/batchRemove")
    public Result<Integer> batchRemove(@RequestBody List<Long> ids) throws Exception {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(roleService.batchDelete(ids));
    }
}
